#!/usr/bin/env bash
# tribute to: https://github.com/tatou-tatou/Themes/blob/master/Stendhal/Previews/icon-rightclick.gif
# <repeat> ? S ->
# assuming repeat, random, single mode, consume mode

# 0 nothing, 1 controls, 2 options
target=/tmp/state
[ ! -e $target ] && echo 0 > $target
state=$(cat $target)

meta start
meta mouse_left echo "$(( (state+1) % 3 ))"' > '"$target"

action_wrap() {
    condition=$(iif "[ \"$1\" = \"on\" ]")
    action="$2"
    content="$3"

    $condition && meta active
    meta mouse_left mpc $action $(iif $condition off on)
    echo $content
    $condition && meta reset
}

iicon() {
    echo "$(p_icon_enable=true icon "$*")"
}

#notify-send "$state"

if [ $state -eq 0 ]; then
    mpc | grep playing && iicon pause || iicon music
    meta end
fi

if [ $state -eq 1 ]; then
    echo Mu
    # prev pause next down
    meta mouse_left "mpc prev"
    iicon prev

    meta mouse_left "mpc toggle"
    mpc | grep playing && iicon pause || iicon play

    meta mouse_left "mpc next"
    iicon next

    meta end
fi

if [ $state -eq 2 ]; then
    echo Mu
    eval "$(mpc | tail -n 1 | sed 's/: /=/g')"
    action_wrap "$repeat"  "repeat"  "$(iicon rotate)"
    action_wrap "$random"  "random"  "?"
    action_wrap "$single"  "single"  "S"
    action_wrap "$consume" "consume" "->"
    meta end
fi
